﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CaseManager : MonoBehaviour
{
    [SerializeField] public int NeedSaveBird  = 0;
    [SerializeField] public string thisCaseName = "";
    [SerializeField] public string nextCaseName = "";
    [SerializeField] private Transform Mask;
    [SerializeField] private float maskSpeed=1;
    [SerializeField] private GameObject gameEndUI;
    [SerializeField] private GameObject gameEndMask;
    private Transform followTransform;
    private Vector3 endSize=Vector3.zero;
    private bool gameEnd = false;
    public int SavedBird { get; private set; } = 0;
    private void Start()
    {
        EventManager.Add(EventNameConfig.GanmeEnd.PlayerInEndLight, PlayerInEndLight);
        EventManager.Add(EventNameConfig.GanmeEnd.PlayerOutEndLight, PlayerOutEndLight);
        EventManager.Add(EventNameConfig.GanmeEnd.GanmeEndStr, GameEnd);
        gameEndMask.SetActive(true);

        if (SaveData._.CasePass.ContainsKey(thisCaseName))
        {
            SaveData._.CasePass[thisCaseName] = true;
            SaveData.Save();
        }
        GetComponent<AudioSource>().volume = SaveData._.BGMSize;
    }

    public void RePlayThisCase()
    {
        LoadScene_Async.LoadScene(thisCaseName);
    }

    public void GoNextCase()
    {
        Debug.Log(nextCaseName + " " + gameObject.name);
        if (SavedBird<NeedSaveBird)
        {
            //TODO 未救完所有鸟提示
        }
        else
        {
           
            LoadScene_Async.LoadScene(nextCaseName);
        }
    }

    public void SaveOneBird(string name="")
    {
        SavedBird++;
        if (SaveData._.BirdInfo.ContainsKey(name))
        {
            SaveData._.BirdInfo[name] = true;
            SaveData.Save();
        }
    }


    bool ReadyEnd = false;
    bool ReadyReStart = false;
    Vector3 EndMaskS = new Vector3(10, 10, 10);
    Vector3 EndMaskE = new Vector3(1, 1, 1);


    private void Update()
    {
        if (gameEnd)
        {
            return;
        }
        CaseReadyEnd();
        CaseReadyReStart();
    }

    private void CaseReadyEnd()
    {
        if (ReadyEnd)
        {
            if (followTransform!=null)
            {
                Mask.position = followTransform.position;
            }
            //Mask.localScale = Vector3.Lerp(Mask.localScale, EndMaskE, maskSpeed*Time.deltaTime);
            Mask.localScale = new Vector3(Mask.localScale.x - maskSpeed * Time.deltaTime, Mask.localScale.y - maskSpeed * Time.deltaTime, 1);

            if (Mask.localScale.x<= endSize.x&& Mask.localScale.y <= endSize.y)
            {
                Mask.localScale = endSize;
                ReadyEnd = false;
                EventManager.Invoke(EventNameConfig.GanmeEnd.GanmeEndStr);
            }
        }
    }

    private void CaseReadyReStart()
    {
        if (ReadyReStart)
        {
            if (followTransform != null)
            {
                Mask.position = followTransform.position;
            }
            //Mask.localScale = Vector3.Lerp(Mask.localScale, EndMaskS, maskSpeed * Time.deltaTime);
            Mask.localScale = new Vector3(Mask.localScale.x + maskSpeed * Time.deltaTime, Mask.localScale.y + maskSpeed * Time.deltaTime, 1);
            if (Mask.localScale.x >= EndMaskS.x && Mask.localScale.y >= EndMaskS.y)
            {
                Mask.localScale = EndMaskS;
                ReadyReStart = false;
            }
        }
    }

    private void PlayerInEndLight(params object[] o)
    {
        if (o != null && o.Length == 1 && o[0] != null)
        {
            followTransform = (Transform)o[0];
            endSize = Vector3.zero;
        }
        else if(o!=null&&o.Length==2)
        {
            Mask.position = (Vector3)o[0];
            float distance = (float)o[1];
            endSize = new Vector3(distance,distance,0);
            //停止控制
            EventManager.Invoke("gameover");
        }
        ReadyEnd = true;
        ReadyReStart = false;
    }

    private void PlayerOutEndLight(params object[] o)
    {
        ReadyEnd = false;
        ReadyReStart = true;
    }

    private void GameEnd(params object[] o)
    {
        //停止控制
        EventManager.Invoke("gameover");
        gameEndUI.SetActive(true);
        gameEndUI.GetComponent<Animator>().SetBool("isShow", true);
        gameEnd = true;
    }

    private void OnDestroy()
    {
        EventManager.Reomve(EventNameConfig.GanmeEnd.PlayerInEndLight, PlayerInEndLight);
        EventManager.Reomve(EventNameConfig.GanmeEnd.PlayerOutEndLight, PlayerOutEndLight);
        EventManager.Reomve(EventNameConfig.GanmeEnd.GanmeEndStr, GameEnd);

    }
}
